from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from starlette.staticfiles import StaticFiles
from fastapi.openapi.docs import get_swagger_ui_html
from typing import Union
from app.routes.ClasRoute import clas_router
from app.routes.StudentRoute import student_route

app = FastAPI(docs_url=None)
app.include_router(clas_router, prefix="/api")
app.include_router(student_route, prefix="/api")


# 挂载静态文件路径
app.mount("/asserts", StaticFiles(directory="app/asserts"), name="asserts")
# 添加 CORS 中间件，允许所有来源

@app.get("/docs", include_in_schema=False)
async def custom_swagger_ui_html():
    return get_swagger_ui_html(
        openapi_url=app.openapi_url,
        title="Custom Swagger UI",
        swagger_js_url="/asserts/swagger-ui-bundle.js",
        swagger_css_url="/asserts/swagger-ui.css"
    )

# 添加 CORS 中间件，允许所有来源
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],             # 允许所有来源
    allow_credentials=True,          # 允许携带凭证（如 cookies）
    allow_methods=["*"],             # 允许所有 HTTP 方法（GET, POST, PUT 等）
    allow_headers=["*"],             # 允许所有请求头
)

# @app.get("/users/{user_id}" ,summary="路径参数测试" )
# def test(user_id: int):
#     if user_id <= 0:
#        return {
#              "error": "Invalid user ID",
#              "message": "User ID must be a positive integer"
#         }
#     else:
#         return {
#             "user_id": user_id,
#             "message": "User ID is valid"
#         }
#
# @app.get("/search",summary="查询参数测试")
# def search(name: str , age: int):
#     return {"name":name,"age":age}
#


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, host="0.0.0.0", port=8888)